package com.jmgd.cjlx.config;

import com.jmgd.cjlx.constants.DzApplicationCommon;
import com.jmgd.cjlx.constants.GyApplicationCommon;
import com.jmgd.cjlx.controller.DzController;
import com.jmgd.cjlx.dao.JmgdMapper;
import com.jmgd.cjlx.service.*;
import com.jmgd.cjlx.util.EncodingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Configuration
@EnableScheduling
public class ScheduleConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleConfiguration.class);

//    @Autowired
//    DzService dzService;

    @Autowired
    DzGyService dzGyService;

    @Autowired
    JmgdMapper jmgdMapper;

    @Autowired
    JztService jztService;

    @Autowired
    JmgdService jmgdService;

    @Autowired
    MeTuanService meTuanService;


    /**
     * 每天凌晨2点拉取
     * @throws Exception
     */
    //@Scheduled(cron = "0 0 5 * * ?")
    @Scheduled(cron = "0 0 7 * * ?")
    public void qureyStockAndPrice() throws Exception{
        logger.info("大众与国盈每天拉取供应商库存和价格");
        dzGyService.queryPriceBatch(DzApplicationCommon.APPID,DzApplicationCommon.APPSECRET,DzApplicationCommon.SUPPLIERID,DzApplicationCommon.URL,"大众");
        dzGyService.queryStockBatch(DzApplicationCommon.APPID,DzApplicationCommon.APPSECRET,DzApplicationCommon.SUPPLIERID,DzApplicationCommon.URL,"大众");

        dzGyService.queryPriceBatch(GyApplicationCommon.APPID,GyApplicationCommon.APPSECRET,GyApplicationCommon.SUPPLIERID,GyApplicationCommon.URL,"国盈");
        dzGyService.queryStockBatch(GyApplicationCommon.APPID,GyApplicationCommon.APPSECRET,GyApplicationCommon.SUPPLIERID,GyApplicationCommon.URL,"国盈");

        logger.info("九州通每天拉取供应商库存和价格");

        jztService.getWareKcAndPrice();

        jmgdMapper.updateWarePrice();

        logger.info("九州通增加商品列表");

        jztService.addDrug();
    }



    /**
     * 每天凌晨2点拉取
     * @throws Exception
     */
    //@Scheduled(cron = "0 0 5 * * ?")
    @Scheduled(cron = "0 0 6 * * ?")
    public void ykbls() throws Exception{
        jmgdService.ykbls();
    }


    @Scheduled(cron = "0 0 18 * * ?")
    public void mt1() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 19 * * ?")
    public void mt2() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 20 * * ?")
    public void mt3() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 21 * * ?")
    public void mt4() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 22 * * ?")
    public void mt5() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 23 * * ?")
    public void mt6() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void mt7() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountList();
        System.out.println("美团下载结束");
    }


    @Scheduled(cron = "0 0 1 * * ?")
    public void mtOther1() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 2 * * ?")
    public void mtOther2() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void mtOther3() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 4 * * ?")
    public void mtOther4() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 5 * * ?")
    public void mtOther5() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 7 * * ?")
    public void mtOther6() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void mtOther7() throws Exception{
        System.out.println("美团下载开始");
        meTuanService.actRetailDiscountListOther();
        System.out.println("美团下载结束");
    }


//
//    @Autowired
//    PosService posService;
//
//    @Autowired
//    JmgdService jmgdService;
//
//    @Autowired
//    LogService logService;
//
//
//    /**
//     * 每天2点15分删除万店掌3月前订单
//     * @throws Exception
//     */
//    @Scheduled(cron = "0 15 02 * * ?")
//    public void delOrder() throws Exception{
//        Date dNow = new Date();
//        Date dBefore = new Date();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(dNow);
//        calendar.add(2, -3);
//        dBefore = calendar.getTime();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println("定时删除订单记录");
//        posService.delOrder(sdf.format(dBefore),sdf.format(dBefore));
//        logService.insertLog("万店掌","每天定时删除订单成功");
//
//    }
//
//
//    /**
//     * 每月1号9点生成流向excel文件
//     * @throws Exception
//     */
//    @Scheduled(cron = "0 0 10 1 * ?")
//    public void createExcelLx() throws Exception{
//        System.out.println("每月1号9点生成流向excel文件");
//        jmgdService.createExcelLx(null,"1");
//    }
//
//    /**
//     * 每天4点拉取前两天已开发票
//     * @throws Exception
//     */
//    @Scheduled(cron = "0 0 4 * * ?")
//    public void queryInvoiceYk() throws Exception{
//        Date dNow = new Date();
//        Date dBefore = new Date();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(dNow);
//        calendar.add(Calendar.DAY_OF_MONTH, -90);
//        dBefore = calendar.getTime();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String beginDateTime = sdf.format(dBefore)+" 00:00:00";
//        String endDateTime = sdf.format(dNow)+" 23:59:59";
//
//        System.out.println("定时拉取已开发票");
//        jmgdService.queryInvoiceYk(beginDateTime,endDateTime,null);
//        logService.insertLog("发票","每天定时拉取发票成功");
//    }
//
//    /**
//     * 每天5点拉取商品分类税收编码
//     * @throws Exception
//     */
//    @Scheduled(cron = "0 0 5 * * ?")
//    public void queryWareTaxCode() throws Exception{
//        Date dNow = new Date();
//        Date dBefore = new Date();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(dNow);
//        calendar.add(Calendar.DAY_OF_MONTH, -2);
//        dBefore = calendar.getTime();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String beginDateTime = sdf.format(dBefore)+" 00:00:00";
//        String endDateTime = sdf.format(dNow)+" 23:59:59";
//
//        System.out.println("每天定时拉取商品税收编码成功");
//        jmgdService.queryTaxCodeByWare(null,beginDateTime);
//        logService.insertLog("发票","每天定时拉取商品税收编码成功");
//    }
//
//    /**
//     * 插入会员异常手机号码
//     * @throws Exception
//     */
//    @Scheduled(cron = "0 0 4 * * ?")
//    public void insertExcpMemMobile() throws Exception{
//        System.out.println("每天定时插入会员异常手机号码");
//        jmgdService.checkMemcardMobile();
//    }


    public static void main(String[] args){
//        Date dNow = new Date();
////        Date dBefore = new Date();
////        Calendar calendar = Calendar.getInstance();
////        calendar.setTime(dNow);
////        calendar.add(2, -3);
////        dBefore = calendar.getTime();
////        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
////        System.out.println(sdf.format(dBefore));
//        try {
//            //String b = URLEncoder.encode("beginTime=2022-05-20 00:00:00&endTime=2022-05-20 23:59:59","UTF-8");
//            //String b = URLEncoder.encode("orderId=&beginTime=2022-05-20 00:00:00&endTime=2022-05-20 23:59:59&deptId=&fphm=","UTF-8")+"0ePocogwuda2t7dAleCcKlACG";
//            String b= EncodingUtil.decodeURIComponent("orderId=&beginTime=2022-05-23 00:00:00&endTime=2022-05-23 23:59:59");
//            System.out.println(b);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
